package com.dongdongshop.controller;


import com.dongdongshop.data.Result;
import com.dongdongshop.dto.UserDto;
import com.dongdongshop.enums.ResultEnums;
import com.dongdongshop.service.UserService;
import com.dongdongshop.vo.UserVo;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService us;

    @Autowired
    private RocketMQTemplate mq;

    @Autowired
    private StringRedisTemplate redis;
    //注册
    @PostMapping
    public Result register(@RequestBody UserVo vo){
        //从 redis 中取出验证码   判断验证码是否正确
        String s = redis.opsForValue().get(vo.getPhone());
        if (!s.equals(vo.getPhoneNum())){
            return Result.response(ResultEnums.MESSAGE);
        }
        us.register(vo);
        return Result.OK();
    }

    //发送短信
    @PostMapping("sendMessage")
    public Result sendMessage(@RequestParam String phoneNum){
        mq.convertAndSend("dk2206-topic", phoneNum);
        return Result.OK();
    }

    //登录
    @PostMapping("get")
    public Result<UserDto> getUserByName(@RequestParam("username") String username){
        UserVo vo = us.getUserByName(username);
        return Result.OK().setData(vo);
    }
}
